import { defineStore } from 'pinia'
import axios from 'axios'

export const useUsersStore = defineStore('users', {
  state: () => ({
    patients: [],
    doctors: [],
    loading: false,
    error: null
  }),
  
  actions: {
    // Patient Management
    async fetchPatients() {
      this.loading = true
      this.error = null
      
      try {
        const response = await axios.get('/getAllPatientUsers')
        this.patients = response.data
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to fetch patients'
        console.error('Error fetching patients:', error)
      } finally {
        this.loading = false
      }
    },
    
    async addPatient(patient) {
      this.loading = true
      this.error = null
      
      try {
        await axios.post('/insertPatientUser', patient)
        await this.fetchPatients()
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to add patient'
        console.error('Error adding patient:', error)
      } finally {
        this.loading = false
      }
    },
    
    async updatePatient(patient) {
      this.loading = true
      this.error = null
      
      try {
        await axios.post('/updatePatientUser', patient)
        await this.fetchPatients()
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to update patient'
        console.error('Error updating patient:', error)
      } finally {
        this.loading = false
      }
    },
    
    async deletePatient(pId) {
      this.loading = true
      this.error = null
      
      try {
        await axios.get(`/deletePatientUser/${pId}`)
        await this.fetchPatients()
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to delete patient'
        console.error('Error deleting patient:', error)
      } finally {
        this.loading = false
      }
    },
    
    // Doctor Management
    async fetchDoctors() {
      this.loading = true
      this.error = null
      
      try {
        const response = await axios.get('/getAllDoctorUsers')
        this.doctors = response.data
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to fetch doctors'
        console.error('Error fetching doctors:', error)
      } finally {
        this.loading = false
      }
    },
    
    async fetchDoctorsBySection(section) {
      this.loading = true
      this.error = null
      
      try {
        const response = await axios.get(`/getDoctorUserBySection/${section}`)
        return response.data
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to fetch doctors by section'
        console.error('Error fetching doctors by section:', error)
        return []
      } finally {
        this.loading = false
      }
    },
    
    async addDoctor(doctor) {
      this.loading = true
      this.error = null
      
      try {
        await axios.post('/insertDoctorUser', doctor)
        await this.fetchDoctors()
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to add doctor'
        console.error('Error adding doctor:', error)
      } finally {
        this.loading = false
      }
    },
    
    async updateDoctor(doctor) {
      this.loading = true
      this.error = null
      
      try {
        await axios.post('/updateDoctorUser', doctor)
        await this.fetchDoctors()
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to update doctor'
        console.error('Error updating doctor:', error)
      } finally {
        this.loading = false
      }
    },
    
    async deleteDoctor(dId) {
      this.loading = true
      this.error = null
      
      try {
        await axios.get(`/deleteDoctorUser/${dId}`)
        await this.fetchDoctors()
      } catch (error) {
        this.error = error.response?.data?.message || 'Failed to delete doctor'
        console.error('Error deleting doctor:', error)
      } finally {
        this.loading = false
      }
    }
  }
})

// Create an alias export for useUserStore to maintain compatibility with existing code
export const useUserStore = useUsersStore; 